package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.query.DestinationQuery;
import cn.wolfcode.wolf2w.repository.DestinationRepository;
import cn.wolfcode.wolf2w.repository.RegionRepository;
import cn.wolfcode.wolf2w.repository.StrategyCatalogRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DestinationServiceImpl implements IDestinationService {

    @Autowired
    private DestinationRepository destinationRepository;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private IRegionService regionService;
    @Autowired
    private RegionRepository regionRepository;
    @Autowired
    private StrategyCatalogRepository strategyCatalogRepository;
    @Autowired
    private IStrategyService strategyService;


    @Override
    public void save(Destination destination) {
        destination.setId(null);
        destinationRepository.save(destination);
    }

    @Override
    public void update(Destination destination) {
        destinationRepository.save(destination);
    }

    @Override
    public void delete(String id) {
        destinationRepository.deleteById(id);
    }

    @Override
    public Destination get(String id) {
        return destinationRepository.findById(id).orElse(null);
    }

    @Override
    public List<Destination> listAll() {
        return destinationRepository.findAll();
    }

    @Override
    public Page<Destination> query(DestinationQuery qo) {
        //创建查询对象
        Query query = new Query();
        //模糊查询
        if (StringUtils.hasLength(qo.getKeyword())){
            query.addCriteria(Criteria.where("name").regex(qo.getKeyword()));
        }
        //吐司
        if (StringUtils.hasLength(qo.getParentId())){
            query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        }else {
            query.addCriteria(Criteria.where("parentId").is(null));
        }

        PageRequest pageable = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        return DBHelper.query(template, Destination.class,query, pageable);
    }

    @Override
    public void saveOrUpdate(Destination destination) {

        if (!StringUtils.hasLength(destination.getId())){

            this.save(destination);

        }else {
            this.update(destination);
        }
    }

    //创建toasts
    private void createToast(List<Destination> list,String parentId){

        //parentId为空时不执行,没有父id
        if (!StringUtils.hasLength(parentId)){
            return;
        }
        Destination destination = this.get(parentId);
        list.add(destination);
        if (StringUtils.hasLength(destination.getParentId())){
            createToast(list,destination.getParentId());
        }
    }

    @Override
    public List<Destination> queryToasts(String parentId) {
        if (!StringUtils.hasLength(parentId)){
            return Collections.emptyList();
        }
        List<Destination> list = new ArrayList<>();
        //递归，调用自身，查询子集合
        this.createToast(list,parentId);
        Collections.reverse(list);
        return list;
    }

    @Override
    public List<Destination> queryDestByRegionIdForApi(String regionId) {
        List<Destination> list = new ArrayList<>();
        //区域：国内
        if ("-1".equals(regionId)){
            list = destinationRepository.findByParentName("中国");
        }else {
            //非国内
            list = this.queryDestByRegionId(regionId);
        }

        for (Destination destination : list){
            PageRequest pageable = PageRequest.of(0, 10, Sort.Direction.ASC, "_id");
            List<Destination> children = destinationRepository.findByParentId(destination.getId(),pageable);
            destination.setChildren(children);
        }

        return list;
    }



    @Override
    public List<StrategyCatalog> queryDetals(String id) {

        return null;
    }

    @Override
    public Destination queryByName(String keyword) {
        return destinationRepository.findByName(keyword);
    }

    @Override
    public List<Destination> queryDestByRegionId(String rid) {
        //通过区域id查询区域对象
        Region region = regionService.get(rid);
        //获取对象挂载的目的集合
        List<String> ids = region.getRefIds();
        return destinationRepository.findByIdIn(ids);
    }



}
